package c.f.d.a.a.b.g;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.Uri;
import android.os.Build.VERSION;
import android.text.TextUtils;
import android.util.Log;
import com.huawei.hms.aaid.constant.AaidIdConstant;
import com.pgl.sys.ces.out.ISdkLite;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;

public class a {
    public static final String a = "a";
    public static final Uri b = Uri.parse("content://com.huawei.hwid");
    public static final String[] c = new String[]{"B92825C2BD5D6D6D1E7F39EECD17843B7D9016F611136B75441BC6F4D3F00F05", "E49D5C2C0E11B3B1B96CA56C6DE2A14EC7DAB5CCC3B5F300D03E5B4DBA44F539"};

    public static String a(byte[] bArr) {
        String str = "";
        if (bArr == null || bArr.length == 0) {
            return str;
        }
        try {
            bArr = MessageDigest.getInstance(AaidIdConstant.SIGNATURE_SHA256).digest(bArr);
            if (bArr == null) {
                return str;
            }
            if (bArr.length == 0) {
                return str;
            }
            StringBuilder stringBuilder = new StringBuilder();
            for (byte b : bArr) {
                String toHexString = Integer.toHexString(b & ISdkLite.REGION_UNSET);
                if (toHexString.length() == 1) {
                    stringBuilder.append('0');
                }
                stringBuilder.append(toHexString);
            }
            return stringBuilder.toString();
        } catch (NoSuchAlgorithmException e) {
            String str2 = a;
            StringBuilder g = c.b.a.a.a.g("NoSuchAlgorithmException");
            g.append(e.getMessage());
            Log.e(str2, g.toString());
            return str;
        }
    }

    /* JADX WARNING: Missing exception handler attribute for start block: B:29:0x0074 */
    /* JADX WARNING: Failed to process nested try/catch */
    /* JADX WARNING: Can't wrap try/catch for region: R(6:27|26|29|30|31|32) */
    /* JADX WARNING: Missing block: B:28:0x0072, code skipped:
            r5 = th;
     */
    /* JADX WARNING: Missing block: B:33:0x007f, code skipped:
            r1 = r6;
     */
    public static void b(java.io.InputStream r5, android.content.Context r6) {
        /*
        if (r5 == 0) goto L_0x0084;
    L_0x0002:
        r6 = c(r6);
        r0 = new java.io.File;
        r0.<init>(r6);
        r0 = r0.exists();
        if (r0 != 0) goto L_0x003d;
    L_0x0011:
        r0 = android.text.TextUtils.isEmpty(r6);
        if (r0 == 0) goto L_0x0018;
    L_0x0017:
        goto L_0x003d;
    L_0x0018:
        r0 = new java.io.File;
        r0.<init>(r6);
        r1 = r0.exists();
        if (r1 == 0) goto L_0x002f;
    L_0x0023:
        r0 = a;
        r0 = c.f.a.a.a.a.l(r0);
        r1 = "The directory  has already exists";
        android.util.Log.w(r0, r1);
        goto L_0x003d;
    L_0x002f:
        r0 = r0.mkdirs();
        if (r0 == 0) goto L_0x0036;
    L_0x0035:
        goto L_0x003d;
    L_0x0036:
        r0 = a;
        r1 = "create directory  failed";
        c.f.a.a.a.a.k(r0, r1);
    L_0x003d:
        r0 = new java.io.File;
        r1 = "hmsrootcas.bks";
        r0.<init>(r6, r1);
        r6 = r0.exists();
        if (r6 == 0) goto L_0x004d;
    L_0x004a:
        r0.delete();
    L_0x004d:
        r6 = 0;
        r1 = a;	 Catch:{ IOException -> 0x0074 }
        r2 = "write output stream ";
        c.f.a.a.a.a.h(r1, r2);	 Catch:{ IOException -> 0x0074 }
        r1 = new java.io.FileOutputStream;	 Catch:{ IOException -> 0x0074 }
        r1.<init>(r0);	 Catch:{ IOException -> 0x0074 }
        r6 = 2048; // 0x800 float:2.87E-42 double:1.0118E-320;
        r0 = new byte[r6];	 Catch:{ IOException -> 0x0070, all -> 0x006e }
    L_0x005e:
        r2 = 0;
        r3 = r5.read(r0, r2, r6);	 Catch:{ IOException -> 0x0070, all -> 0x006e }
        r4 = -1;
        if (r3 == r4) goto L_0x006a;
    L_0x0066:
        r1.write(r0, r2, r3);	 Catch:{ IOException -> 0x0070, all -> 0x006e }
        goto L_0x005e;
    L_0x006a:
        c.f.a.a.a.a.c(r1);
        goto L_0x007e;
    L_0x006e:
        r5 = move-exception;
        goto L_0x0080;
    L_0x0070:
        r6 = r1;
        goto L_0x0074;
    L_0x0072:
        r5 = move-exception;
        goto L_0x007f;
    L_0x0074:
        r5 = a;	 Catch:{ all -> 0x0072 }
        r0 = " IOException";
        c.f.a.a.a.a.k(r5, r0);	 Catch:{ all -> 0x0072 }
        c.f.a.a.a.a.c(r6);
    L_0x007e:
        return;
    L_0x007f:
        r1 = r6;
    L_0x0080:
        c.f.a.a.a.a.c(r1);
        throw r5;
    L_0x0084:
        return;
        */
        throw new UnsupportedOperationException("Method not decompiled: c.f.d.a.a.b.g.a.b(java.io.InputStream, android.content.Context):void");
    }

    public static String c(Context context) {
        String str = "aegis";
        StringBuilder stringBuilder;
        if (VERSION.SDK_INT >= 24) {
            stringBuilder = new StringBuilder();
            stringBuilder.append(context.createDeviceProtectedStorageContext().getFilesDir());
            return c.b.a.a.a.d(stringBuilder, File.separator, str);
        }
        stringBuilder = new StringBuilder();
        stringBuilder.append(context.getApplicationContext().getFilesDir());
        return c.b.a.a.a.d(stringBuilder, File.separator, str);
    }

    public static boolean d(Context context) {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(c(context));
        return new File(c.b.a.a.a.d(stringBuilder, File.separator, "hmsrootcas.bks")).exists();
    }

    public static boolean e(String str) {
        boolean z = false;
        if (TextUtils.isEmpty(str)) {
            return false;
        }
        String str2 = a;
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("hms version code is : ");
        stringBuilder.append(str);
        c.f.a.a.a.a.h(str2, stringBuilder.toString());
        str2 = "\\.";
        String[] split = str.split(str2);
        String[] split2 = "4.0.2.300".split(str2);
        int length = split.length;
        int length2 = split2.length;
        int max = Math.max(length, length2);
        for (int i = 0; i < max; i++) {
            int parseInt;
            int parseInt2;
            if (i < length) {
                try {
                    parseInt = Integer.parseInt(split[i]);
                } catch (Exception e) {
                    str2 = a;
                    stringBuilder = c.b.a.a.a.g(" exception : ");
                    stringBuilder.append(e.getMessage());
                    c.f.a.a.a.a.k(str2, stringBuilder.toString());
                    if (i >= length2) {
                        z = true;
                    }
                    return z;
                }
            }
            parseInt = 0;
            if (i < length2) {
                parseInt2 = Integer.parseInt(split2[i]);
            } else {
                parseInt2 = 0;
            }
            if (parseInt < parseInt2) {
                return false;
            }
            if (parseInt > parseInt2) {
                return true;
            }
        }
        return true;
    }

    public static byte[] f(Context context, String str) {
        StringBuilder g;
        if (context == null || TextUtils.isEmpty(str)) {
            Log.e(a, "packageName is null or context is null");
            return new byte[0];
        }
        try {
            PackageManager packageManager = context.getPackageManager();
            if (packageManager != null) {
                PackageInfo packageInfo = packageManager.getPackageInfo(str, 64);
                if (packageInfo != null) {
                    return packageInfo.signatures[0].toByteArray();
                }
            }
        } catch (NameNotFoundException e) {
            str = a;
            g = c.b.a.a.a.g("PackageManager.NameNotFoundException : ");
            g.append(e.getMessage());
            Log.e(str, g.toString());
        } catch (Exception e2) {
            str = a;
            g = c.b.a.a.a.g("get pm exception : ");
            g.append(e2.getMessage());
            Log.e(str, g.toString());
        }
        return new byte[0];
    }

    /* JADX WARNING: Missing exception handler attribute for start block: B:58:0x0104 */
    /* JADX WARNING: Can't wrap try/catch for region: R(4:58|59|60|61) */
    /* JADX WARNING: Missing block: B:59:?, code skipped:
            c.f.a.a.a.a.k(a, "Get bks from HMS_VERSION_CODE exception : No content provider");
     */
    /* JADX WARNING: Missing block: B:61:0x010c, code skipped:
            return null;
     */
    public static synchronized java.io.InputStream g(android.content.Context r14) {
        /*
        r0 = c.f.d.a.a.b.g.a.class;
        monitor-enter(r0);
        r1 = a;	 Catch:{ all -> 0x010d }
        r2 = "get bks from tss begin";
        c.f.a.a.a.a.h(r1, r2);	 Catch:{ all -> 0x010d }
        if (r14 == 0) goto L_0x000f;
    L_0x000c:
        c.f.a.a.a.a.A(r14);	 Catch:{ all -> 0x010d }
    L_0x000f:
        r14 = c.f.a.a.a.a.a;	 Catch:{ all -> 0x010d }
        r2 = 0;
        if (r14 != 0) goto L_0x001b;
    L_0x0014:
        r14 = "context is null";
        c.f.a.a.a.a.k(r1, r14);	 Catch:{ all -> 0x010d }
        monitor-exit(r0);
        return r2;
    L_0x001b:
        r3 = "com.huawei.hwid";
        r3 = c.f.d.a.a.b.g.c.a(r3);	 Catch:{ all -> 0x010d }
        if (r3 != 0) goto L_0x0032;
    L_0x0023:
        r3 = "com.huawei.hms";
        r3 = c.f.d.a.a.b.g.c.a(r3);	 Catch:{ all -> 0x010d }
        if (r3 != 0) goto L_0x0032;
    L_0x002b:
        r14 = "hms not exist";
        c.f.a.a.a.a.k(r1, r14);	 Catch:{ all -> 0x010d }
        monitor-exit(r0);
        return r2;
    L_0x0032:
        r3 = "com.huawei.hwid";
        r3 = c.f.d.a.a.b.g.c.b(r3);	 Catch:{ all -> 0x010d }
        r3 = e(r3);	 Catch:{ all -> 0x010d }
        if (r3 != 0) goto L_0x0066;
    L_0x003e:
        r3 = "com.huawei.hms";
        r3 = c.f.d.a.a.b.g.c.b(r3);	 Catch:{ all -> 0x010d }
        r3 = e(r3);	 Catch:{ all -> 0x010d }
        if (r3 != 0) goto L_0x0066;
    L_0x004a:
        r14 = new java.lang.StringBuilder;	 Catch:{ all -> 0x010d }
        r14.<init>();	 Catch:{ all -> 0x010d }
        r3 = "hms version code is too low : ";
        r14.append(r3);	 Catch:{ all -> 0x010d }
        r3 = "com.huawei.hwid";
        r3 = c.f.d.a.a.b.g.c.b(r3);	 Catch:{ all -> 0x010d }
        r14.append(r3);	 Catch:{ all -> 0x010d }
        r14 = r14.toString();	 Catch:{ all -> 0x010d }
        c.f.a.a.a.a.k(r1, r14);	 Catch:{ all -> 0x010d }
        monitor-exit(r0);
        return r2;
    L_0x0066:
        r1 = "com.huawei.hwid";
        r1 = f(r14, r1);	 Catch:{ all -> 0x010d }
        r3 = c;	 Catch:{ all -> 0x010d }
        r4 = r3.length;	 Catch:{ all -> 0x010d }
        r5 = 0;
        r6 = 0;
    L_0x0071:
        if (r6 >= r4) goto L_0x0084;
    L_0x0073:
        r7 = r3[r6];	 Catch:{ all -> 0x010d }
        r8 = a(r1);	 Catch:{ all -> 0x010d }
        r7 = r7.equalsIgnoreCase(r8);	 Catch:{ all -> 0x010d }
        if (r7 == 0) goto L_0x0081;
    L_0x007f:
        r5 = 1;
        goto L_0x0084;
    L_0x0081:
        r6 = r6 + 1;
        goto L_0x0071;
    L_0x0084:
        if (r5 != 0) goto L_0x00a1;
    L_0x0086:
        r1 = "com.huawei.hms";
        r1 = f(r14, r1);	 Catch:{ all -> 0x010d }
        r1 = a(r1);	 Catch:{ all -> 0x010d }
        r3 = "E49D5C2C0E11B3B1B96CA56C6DE2A14EC7DAB5CCC3B5F300D03E5B4DBA44F539";
        r1 = r3.equalsIgnoreCase(r1);	 Catch:{ all -> 0x010d }
        if (r1 != 0) goto L_0x00a1;
    L_0x0098:
        r14 = a;	 Catch:{ all -> 0x010d }
        r1 = "hms sign error";
        c.f.a.a.a.a.k(r14, r1);	 Catch:{ all -> 0x010d }
        monitor-exit(r0);
        return r2;
    L_0x00a1:
        r1 = b;	 Catch:{ Exception -> 0x0104 }
        r3 = "files/hmsrootcas.bks";
        r1 = android.net.Uri.withAppendedPath(r1, r3);	 Catch:{ Exception -> 0x0104 }
        r3 = r14.getContentResolver();	 Catch:{ Exception -> 0x0104 }
        r1 = r3.openInputStream(r1);	 Catch:{ Exception -> 0x0104 }
        r3 = "last_update_time";
        r4 = c.f.d.a.a.b.g.d.a(r14);	 Catch:{ Exception -> 0x0104 }
        r5 = 0;
        r3 = r4.getLong(r3, r5);	 Catch:{ Exception -> 0x0104 }
        r7 = java.lang.System.currentTimeMillis();	 Catch:{ Exception -> 0x0104 }
        r9 = d(r14);	 Catch:{ Exception -> 0x0104 }
        if (r9 == 0) goto L_0x00de;
    L_0x00c7:
        r9 = r7 - r3;
        r11 = 604800000; // 0x240c8400 float:3.046947E-17 double:2.988109026E-315;
        r13 = (r9 > r11 ? 1 : (r9 == r11 ? 0 : -1));
        if (r13 > 0) goto L_0x00de;
    L_0x00d0:
        r9 = (r3 > r5 ? 1 : (r3 == r5 ? 0 : -1));
        if (r9 != 0) goto L_0x00d5;
    L_0x00d4:
        goto L_0x00de;
    L_0x00d5:
        r14 = a;	 Catch:{ Exception -> 0x0104 }
        r3 = "load bks from hms core";
        c.f.a.a.a.a.h(r14, r3);	 Catch:{ Exception -> 0x0104 }
        monitor-exit(r0);
        return r1;
    L_0x00de:
        r3 = "last_update_time";
        r4 = c.f.d.a.a.b.g.d.a(r14);	 Catch:{ Exception -> 0x0104 }
        r4 = r4.edit();	 Catch:{ Exception -> 0x0104 }
        r3 = r4.putLong(r3, r7);	 Catch:{ Exception -> 0x0104 }
        r3.apply();	 Catch:{ Exception -> 0x0104 }
        b(r1, r14);	 Catch:{ Exception -> 0x0104 }
        r3 = a;	 Catch:{ Exception -> 0x0104 }
        r4 = "save last bks and update bks timestamp";
        c.f.a.a.a.a.h(r3, r4);	 Catch:{ Exception -> 0x0104 }
        if (r1 == 0) goto L_0x00fe;
    L_0x00fb:
        c.f.a.a.a.a.g(r1);	 Catch:{ Exception -> 0x0104 }
    L_0x00fe:
        r14 = h(r14);	 Catch:{ Exception -> 0x0104 }
        monitor-exit(r0);
        return r14;
    L_0x0104:
        r14 = a;	 Catch:{ all -> 0x010d }
        r1 = "Get bks from HMS_VERSION_CODE exception : No content provider";
        c.f.a.a.a.a.k(r14, r1);	 Catch:{ all -> 0x010d }
        monitor-exit(r0);
        return r2;
    L_0x010d:
        r14 = move-exception;
        monitor-exit(r0);
        throw r14;
        */
        throw new UnsupportedOperationException("Method not decompiled: c.f.d.a.a.b.g.a.g(android.content.Context):java.io.InputStream");
    }

    public static InputStream h(Context context) {
        if (d(context)) {
            String str = a;
            StringBuilder g = c.b.a.a.a.g("load bks from aegis folder , time is : ");
            g.append(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Long.valueOf(d.a(context).getLong("last_update_time", 0))));
            c.f.a.a.a.a.h(str, g.toString());
            try {
                g = new StringBuilder();
                g.append(c(context));
                g.append(File.separator);
                g.append("hmsrootcas.bks");
                return new FileInputStream(g.toString());
            } catch (FileNotFoundException unused) {
                c.f.a.a.a.a.k(a, "FileNotFoundExceptio: ");
            }
        }
        return null;
    }
}
